package com.qj.yantaishan.common.utils;

import com.alibaba.fastjson.JSON;
import com.qj.yantaishan.common.utils.http.HttpUtils;
import lombok.SneakyThrows;
import org.apache.poi.ss.formula.functions.T;
import org.phprpc.util.PHPSerializer;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * php序列化工具
 */
public class PhpSerializerUtils {
    /*
     * unicode编码
     */
    public static String stringToUnicode(String str) {
        char[] utfBytes = str.toCharArray();
        StringBuilder unicodeBytes = new StringBuilder();
        for (char utfByte : utfBytes) {
            String hexB = Integer.toHexString(utfByte);
            if (hexB.length() <= 2) {
                hexB = "00" + hexB;
            }
            unicodeBytes.append("\\u").append(hexB);
        }
        return unicodeBytes.toString();
    }

    /*
     * unicode解码方式1
     */
    public static String unicodeToString1(String str) {
        int start = 0;
        int end = 0;
        StringBuilder buffer = new StringBuilder();
        while (start > -1) {
            end = str.indexOf("\\u", start + 2);
            String charStr = "";
            if (end == -1) {
                charStr = str.substring(start + 2, str.length());
            } else {
                charStr = str.substring(start + 2, end);
            }
            char letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。
            buffer.append(Character.toString(letter));
            start = end;
        }
        return buffer.toString();
    }

    /**
     * Unicode解码方式2
     */
    private static String unicodeToString2(String str) {
        Pattern pattern = Pattern.compile("(\\\\u(\\w{4}))");
        Matcher matcher = pattern.matcher(str);
        char ch;
        while (matcher.find()) {
            // 本行为核心代码，处理当前的unicode后4位变为16进制，在转换为对应的char中文字符
            ch = (char) Integer.parseInt(matcher.group(2), 16);
            str = str.replace(matcher.group(1), ch + "");
        }
        return str;
    }

    @SneakyThrows
    public static Object unSerializer(String obj, Class cls) {
        if (StringUtils.isEmpty(obj)) throw new NullPointerException("传入要反序列化的值不能为空");
        org.phprpc.util.PHPSerializer ps = new PHPSerializer();
        Object result = ps.unserialize(obj.getBytes(), cls);

        return result;
    }

    @SneakyThrows
    public static String serializer(Object t) {
        if (t == null) throw new NullPointerException("传入要序列化的值不能为空");
        String objArray = objectToPhpSerializer(t);

        return objArray;
    }


    @SneakyThrows
    static String objectToPhpSerializer(Object obj) {
        PHPSerializer phpSerializer = new PHPSerializer();
        Map<String, String> map = new HashMap<>();
        if (obj == null) throw new NullPointerException("你传个屁，空的？");
        //获取到每个字段 和对应的值
        Field[] fields = obj.getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            //获取字段的值
            String value = field.get(obj).toString();
            //获取字段属性名称
            String name = field.getName();
            map.put(name, value);
        }
        byte[] serialize = phpSerializer.serialize(map);
        return new String(serialize);
    }


    public static String serial(String str) {

        String s = HttpUtils.sendPost("https://hqxcx.yantaisan.com/seria.php", "data=" + str);
        return s;
    }

    public static String unSerial(String str) {
        String s = HttpUtils.sendPost("https://hqxcx.yantaisan.com/unseria.php", "data=" + str);
        String s1 = PhpSerializerUtils.unicodeToString2(s);
        return s1;
    }
}